<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ include file="../../include/includes.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<meta http-equiv="Cache-Control" content="No-Cache" />
<meta http-equiv="Pragma" content="No-Cache" />
<script type="text/javascript" src="/js/jquery-1.4.2.js"></script>
<script type="text/javascript" src="/js/jquery.json-2.3.min.js"></script>
<script>

$(document).ready(   
		function() {   
		$("#ajaxsubmit").click(function() {   
	        var jsonuserinfo = $.toJSON($('#form1').serializeObject());   
	        alert(jsonuserinfo);   
	        jQuery.ajax( {   
	          type : 'POST',   
	          contentType : 'application/json',   
	          url : '/example/json/add.do',   
	          data : jsonuserinfo,   
	          dataType : 'json',   
	          success : function(data) {  
	        	alert(data.userName);
	            alert("新增成功！");   
	          },   
	          error : function(data) { 
	        	  alert(data);
	            alert("error");   
	          }   
	        });   
	      });
	      
	    });  
      
  	


  //将一个表单的数据返回成JSON对象   
  	$.fn.serializeObject = function() {   
  	  var o = {};   
  	  var a = this.serializeArray();   
  	  $.each(a, function() {   
  	    if (o[this.name]) {   
  	      if (!o[this.name].push) {   
  	        o[this.name] = [ o[this.name] ];   
  	      }   
  	      o[this.name].push(this.value || '');   
  	    } else {   
  	      o[this.name] = this.value || '';   
  	    }   
  	  });   
  	  return o;   
  	};   
  	  
</script>
</head>
<body>
<div id="info"></div>  
<form action="add" method="post" id="form1" name="form1">  
no: <input type="text" name="id"/>  <br />
name：<input type="text" name="userName"/>  <br/>
password：<input type="text" name="password"/>  <br/>
 
<input type="button" value="ajax提交" id="ajaxsubmit"/>  
<input type="button" value="sumbmit提交" id="submit1"/>  
</form>  

</body>
</html>